/**
 * Created by Administrator on 2017/8/25 0025.
 */
showImage = function (value, row, index) {
    var img = row.typePic;
    var imgUrl = "";
    if (img != null && img != '') {
        imgUrl = File_Download_Url + img;
        return [
            '<image src=' + imgUrl + ' style="width:80px;height:80px" />'
        ].join('');
    } else {
        return [
            '<image src=' + $().AbsUrlParse("/static/images/no.png") + ' style="width:80px;height:80px" />'
        ].join('');
    }
};

operateFormatter = function () {
    return [

        '<a class="edit" href="javascript:void(0)" title="编辑">编辑分类</a>' +
        '&nbsp;&nbsp;' +
        '<a class="remove" href="javascript:void(0)" style="color: red">删除分类</a>'
    ].join('');
};

window.operateEvents = {
    'click .edit': function (e, value, row) {
        showModel("updateType-div", function () {
            $("#id").val(row.id);
            $("#typeName").val(row.typeName);
            $("#rank").val(row.rank);
            $("#priceUpdate").val(row.price);
            $("#moneyUpdate").val(row.money);
            $("#proportionateUpdate").val(row.proportionate);
            var imgUrl = File_Download_Url;
            $("#imges").attr("src",imgUrl + row.typePic);
            $("#imges").attr("img-path", row.typePic);
            $("#updateSelect").val(row.cityId);
        })
    },
    'click .remove': function (e, value, row) {
        comment.showOperationTips("确定删除吗？", function () {
            $.post($().AbsUrlParse('/web/type/updateState'), {
                id: row.id
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    }
};


/**
 * 添加
 */
bindClick("#addType", function () {
    showAddCategory();
});

/**
 * 弹出添加框
 */
var showAddCategory = function () {
    showModel("addType-div", function () {
        $("#dumpTypeName").val('');
        $("#price").val('');
        $("#ranks").val('');
        $("#selectId").val('');
        var findCityId = function () {
            $.post($().AbsUrlParse("/web/type/queryByCityId"), {deptPath: 3}, function (id) {
                var select = $("#selectId");
                select.empty();
                select.append('<option value="-1">-请选择-</option>');
                if (id.res.code == 40000) {
                    var allData = id.res.data;
                    for (var i = 0; i < allData.length; i++) {
                        select.append('<option value="' + allData[i].id + '">' + allData[i].name + '</option>');
                    }
                }
            });
        };

        findCityId();
    });
};

/**
 * 添加按钮事件
 */
bindClick("button[data-event=submit-addType]", function (tag, typeId) {
    var images = $("#img").attr("img-path");
    $("#typePic").val(images);

    var select = $("#selectId").val();
    $("#selectIds").val(select);

    var dump = $("#dumpTypeName").val();
    if (dump != null && dump != "") {
        if (images != null && images != "") {
            var form = $("#addTypeFrom");
            var param = form.formToArray();
            $.post($().AbsUrlParse('/web/type/addHomePageType'), param, function (res) {
                if (res.state && res.res.code == 40000) {
                    hint('添加成功！');
                    //关闭model方法
                    closeModel('addType-div');
                    var table = $("#table");
                    table.bootstrapTable("refresh");
                } else {
                    if (res.state == false) {
                        hint(res.msg);
                    } else {
                        hint(res.res.msg);
                    }
                }
            });
        } else {
            hint("请添加图片");
        }
    } else {
        hint("请添加分类名称");
    }
});

/**
 * 修改按钮事件
 */
bindClick("button[data-event=submit-updateType]", function (tag, typeId) {
    var imag = $("#imges").attr("img-path");
    $("#typePicUpdate").val(imag);

    var select = $("#updateSelect").val();
    $("#updateCityId").val(select);

    var form = $("#updateTypeFrom");
    var param = form.formToArray();
    $.post($().AbsUrlParse('/web/type/updateHomePageType'), param, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功！');
            //关闭model方法
            closeModel('updateType-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

function queryTypeName() {
    var goods = $("#typeNameS").val();
    var url = $().AbsUrlParse("/web/type/queryHomePage?name=" + goods);
    var table = $("#table");
    table.bootstrapTable("refresh", {url: url});
}

var PositioningCityId = function () {
    $.post($().AbsUrlParse("/web/type/queryByCityId"), {deptPath: 3}, function (id) {
        var select = $("#updateSelect");
        select.empty();
        select.append('<option value="-1">-请选择-</option>');
        if (id.res.code == 40000) {
            var allData = id.res.data;
            for (var i = 0; i < allData.length; i++) {
                select.append('<option value="' + allData[i].id + '">' + allData[i].name + '</option>');
            }
        }
    });
};
PositioningCityId();

rank =function (value, row, index) {
    return "<a href='javascript:void(0)' onclick='showRank(" + row.rank + "," + row.id + ")'><span>" + row.rank + "</span></a>";
};

/**
 * 点击排名事件
 * @param orderNum
 * @param id
 */
var showRank = function (sorting, id) {
    showModel("showOrderNum-div", function () {
        $("#pid").val(id);
        $("#sorting").val(sorting);
    });
};

/**
 * 判断输入是否为数字
 * @param v
 * @returns {boolean}
 */
function isNumber(v) {
    var str = v.val();
    var reg = parseFloat(str);
    var re = /^\+?[0-9][0-9]*$/;
    if (!re.test(str)) {
        return false;
    }
    return !isNaN(reg);
}

/**
 * 修改轮播推荐排名按钮点击事件
 */
bindClick("button[data-event=merge-orderNum]", function () {
    var form = $("#updateOrderNum");
    if (!form.valid()) {
        return false;
    }
    var sorting = $("#sorting");
    if (!isNumber(sorting)) {
        hint("请填入非负的正整数.");
        return false;
    }
    var params = form.formToArray();
    $.post($().AbsUrlParse('/web/type/updateHomePageType'), params, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功！');
            //关闭model方法
            closeModel('showOrderNum-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});